﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JKF.Substructure;
using JKF.Entities.Model;
using JKF.DataModule.DALFactory;
using System.Data.Common;
using JKF.DataModule;
using Microsoft.Practices.EnterpriseLibrary.Data;
using JKF.DataModule.IDAL;
namespace JKF.BusinessLayer.BLL
{
    public partial class bllDIC_AREA
    {
        /// <summary>
        /// 获取省数据
        /// </summary>
        /// <returns></returns>
        public List<DIC_AREAinfo> GetDIC_AREAProvince()
        {
            const string cacheName = "PageAreaTwo";
            if (!CacheManage.HadCache(cacheName))
            {
                // 获取当前用户所在区域，总店的获取所有
                // 判断当前用户的角色去加载对应的角色下拉列表
                SYS_ROLEinfo roleInfo = new bllSYS_ROLE().GetModel(bllSYS_USER.CurrentUser.ROLE_KEY);
                SHOPinfo shopInfo = new bllSHOP().GetModel(bllSYS_USER.CurrentUser.SHOP_CODE);
                string now_role_name = roleInfo.ROLE_NAME;
                List<DIC_AREAinfo> oList;
                if (SysConfig.HeadShop.Contains(now_role_name))
                {
                    oList = this.GetModelList("AREA_CODE like '__0000'"); // 加载省市数据 
                    CacheManage.AddCache(cacheName, oList);
                }
                else
                {
                    return null;
                }
            }
            return (List<DIC_AREAinfo>)CacheManage.GetCache(cacheName);
        }

        /// <summary>
        /// 获得省份，Used in select province city area
        /// </summary>
        /// <returns></returns>
        public string GetProvince()
        {
            List<DIC_AREAinfo> oList;
            oList = this.GetModelList("AREA_CODE like '__0000'");
            var sb = new StringBuilder();
            foreach (var item in oList)
            {
                sb.AppendFormat("<option value=\"{0}\">{1}</option>", item.AREA_CODE, item.NAME);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 取得省,直辖市
        /// </summary>
        /// <returns></returns>
        public string GetArea()
        {
            //const string cacheName = "PageAreaOne";
            //if (!CacheManage.HadCache(cacheName))
            //{
            // 获取当前用户所在区域，总店的获取所有
            // 判断当前用户的角色去加载对应的角色下拉列表
            SYS_ROLEinfo roleInfo = new bllSYS_ROLE().GetModel(bllSYS_USER.CurrentUser.ROLE_KEY);

            List<DIC_AREAinfo> oList;
            if (roleInfo != null)
            {
                string now_role_name = roleInfo.ROLE_NAME;
                if (SysConfig.HeadShop.Contains(now_role_name))
                {
                    oList = this.GetModelList("AREA_CODE like '__0000'"); // 加载全部
                }
                else if (SysConfig.AreaShop.Contains(now_role_name))
                {
                    string condition = bllSYS_USER.CurrentUser.ADMIN_AREAS;
                    //shopInfo.AREA_CODE.Substring(0, 3) + "000";
                    //String.Format(" AREA_CODE='{0}' ", condition)
                    oList = this.GetModelList(string.Format("AREA_CODE in ({0})", condition));
                }
                else
                {
                    SHOPinfo shopInfo = new bllSHOP().GetModel(bllSYS_USER.CurrentUser.SHOP_CODE);
                    oList = this.GetModelList(String.Format("AREA_CODE='{0}'", shopInfo.AREA_CODE.Substring(0,2)+"0000"));
                }
            }
            else
            {
                oList = this.GetModelList("AREA_CODE like '__0000'");
            }
            //var oList = this.GetModelList("AREA_CODE like '__0000'");

            var sb = new StringBuilder();
            foreach (var item in oList)
            {
                sb.AppendFormat("<option value=\"{0}\">{1}</option>", item.AREA_CODE, item.NAME);
            }
            return sb.ToString();
            //CacheManage.AddCache(cacheName, sb.ToString());
            //}

            //return CacheManage.GetCache(cacheName).ToString();
        }

        /// <summary>
        /// 取得所有区域,根据提交到后台的城市编码
        /// </summary>
        /// <param name="code">城市编码</param>
        /// <returns></returns>
        public List<DIC_AREAinfo> GetArea(string code)
        {

            if (!string.IsNullOrEmpty(code))
            {
                if (code.IndexOf("nu") > -1)
                {
                    return new List<DIC_AREAinfo>();
                }
                string sqlWhere = "";
                switch (code.TrimEnd('0').Length)
                {
                    case 0:
                        return new List<DIC_AREAinfo>();
                    case 1:  
                        sqlWhere = " AREA_CODE like '" + code.TrimEnd('0') + "0__00" + "'   ";
                        break;
                    case 2:
                        sqlWhere = " AREA_CODE like '" + code.TrimEnd('0') + "__00" + "'   ";
                        break;
                    case 3:
                        sqlWhere = " AREA_CODE like '" + code.TrimEnd('0') + "0__" + "'  And AREA_CODE!= " + code.TrimEnd('0') + "01 ";
                        break;
                    case 4:
                        sqlWhere = " AREA_CODE like '" + code.TrimEnd('0') + "__" + "' And AREA_CODE!= " + code.TrimEnd('0') + "01 ";
                        break;
                    case 5:
                        sqlWhere = " AREA_CODE like '" + code.TrimEnd('0') + "0___" + "'  ";
                        break;
                    case 6:
                        sqlWhere = " AREA_CODE like '" + code.TrimEnd('0') + "___" + "'  ";
                        break;
                    case 7:
                        sqlWhere = " AREA_CODE like '" + code.TrimEnd('0') + "00___" + "'  ";
                        break;
                    case 8:
                        sqlWhere = " AREA_CODE like '" + code.TrimEnd('0') + "0___" + "'  ";
                        break;
                    case 9:
                        sqlWhere = " AREA_CODE like '" + code.TrimEnd('0') + "___" + "'  ";
                        break;


                }

                sqlWhere += " AND AREA_CODE!=" + code.ToSqlString(20);


                return this.GetModelList(sqlWhere);
            }
            return new List<DIC_AREAinfo>();
        }
    }
}
